﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Nurse2.View.Admin
{
    public partial class Categorys : System.Web.UI.Page
    {
        Models.functions Con;

        protected void Page_Load(object sender, EventArgs e)
        {
            Con = new Models.functions();
            ShowCategorys();
        }
        //用来把数据挂接到后端数据库
        protected void SaveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string CatName = CatNameTb.Value;
                string Rem =RemarkTb.Value;
                string Query = "insert into CategoryTb1 values('{0}','{1}')";
                Query = string.Format(Query, CatName, Rem);
                Con.setData(Query);
                ErrMsg.InnerText = "房型已添加!";
            }
            catch(Exception Ex)
            {
                ErrMsg.InnerText = Ex.Message;
            }
        }
        //用来显示网格中的数据
        private void ShowCategorys()
        {
            string Query = "select NurseId as Id,NurseName as Categories,NurseRemark from CategoryTb1";
            CategoryGV3.DataSource = Con.GetData(Query);
            CategoryGV3.DataBind();
            CategoryGV3.HeaderRow.Cells[1].Text = "序号";
            CategoryGV3.HeaderRow.Cells[2].Text = "房型";
            CategoryGV3.HeaderRow.Cells[3].Text = "标签";
        }
        int Key = 0;
        //显示选定行的信息于输入框中
        protected void CategoryGV3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Key = Convert.ToInt32(CategoryGV3.SelectedRow.Cells[1].Text);
            CatNameTb.Value = CategoryGV3.SelectedRow.Cells[2].Text;
            RemarkTb.Value = CategoryGV3.SelectedRow.Cells[3].Text;
        }

        protected void EditBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string CatName = CatNameTb.Value;
                string Rem = RemarkTb.Value;
                string Query = "update CategoryTb1 set NurseName='{0}',NurseRemark='{1}'where NurseId={2}";
                Query = string.Format(Query, CatName, Rem,CategoryGV3.SelectedRow.Cells[1].Text);
                Con.setData(Query);
                ShowCategorys();
                ErrMsg.InnerText = "房型已更新!";
            }
            catch (Exception Ex)
            {
                ErrMsg.InnerText = Ex.Message;
            }
        }

        protected void DeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string CatName = CatNameTb.Value;
                string Rem = RemarkTb.Value;
                string Query = "delete from CategoryTb1 where NurseId={0}";
                Query = string.Format(Query,CategoryGV3.SelectedRow.Cells[1].Text);
                Con.setData(Query);
                ShowCategorys();
                ErrMsg.InnerText = "房型已删除!";
            }
            catch (Exception Ex)
            {
                ErrMsg.InnerText = Ex.Message;
            }
        }
    }
}